﻿using CTP.CRM.Core.SharedModel.Enums;
using CTP.CRM.Core.SharedModel.InternalCore.Token;
using System.Security.Claims;

namespace CTP.CRM.Core.SharedModel.Pub
{
    public class TokenGeneratorHelper
    {
        /// <summary>
        /// Generates a JWT token based on the provided claims, expiration time, and secret key.
        /// </summary>
        /// <param name="claims">The list of claims to include in the token.</param>
        /// <param name="expiredTime">The expiration time for the token (optional). If not provided, it defaults to the value specified in the configuration.</param>
        /// <param name="secretKey">The secret key to use for token generation (optional).</param>
        /// <returns>The generated JWT token.</returns>
        public static string GeneratorToken(List<Claim> claims, string expiredTime = null, string secretKey = null)
        {
            // Retrieve the token generator service
            ITokenGeneratorService tokenGenerator = UserCenterServiceProvider.Instance.GetRequiredService<ITokenGeneratorService>();

            // If expiration time is not provided, use the value from configuration
            expiredTime ??= CTPABPCore.Configuration["JWTSettings:ExpiredTime"];

            // Generate the token using the token generator service
            return tokenGenerator.Generator(claims, expiredTime, secretKey).Token;
        }

        /// <summary>
        /// Generates a refresh token based on the provided claims and expiration time.
        /// </summary>
        /// <param name="claims">The list of claims to include in the refresh token.</param>
        /// <param name="expiredRefreshTime">The expiration time for the refresh token (optional). If not provided, it defaults to the value specified in the configuration.</param>
        /// <returns>The generated refresh token.</returns>
        public static string GeneratorRefreshToken(List<Claim> claims, string expiredRefreshTime = null)
        {
            // Retrieve the token generator service
            ITokenGeneratorService tokenGenerator = UserCenterServiceProvider.Instance.GetRequiredService<ITokenGeneratorService>();

            // If expiration time for refresh token is not provided, use the value from configuration
            expiredRefreshTime ??= CTPABPCore.Configuration["JWTSettings:RefreshTime"];

            // If the 'refresh' claim is not already present, add it to the claims list
            if (!claims.Any(x => x.Type == "refresh"))
            {
                claims.Add(new Claim("refresh", "true"));
            }

            // Generate the refresh token using the token generator service
            return tokenGenerator.Generator(claims, expiredRefreshTime).Token;
        }



        /// <summary>
        /// Generates a system token for system-level operations.
        /// </summary>
        /// <returns>The generated system token.</returns>
        public static string GeneratorSystemToken()
        {
            // Retrieve the token generator service and string encryption service
            ITokenGeneratorService tokenGenerator = UserCenterServiceProvider.Instance.GetRequiredService<ITokenGeneratorService>();


            // Throw an exception if either token generator or string encryption service is null
            ThrowHelper.ThrowIfCodition(tokenGenerator is null, "Failed to generate system token. Please contact the administrator!");

            // Define system and user ID constants
            const string system = "System";
            const string id = "6000";

            // Create claims for the system token
            var systemClaims = new List<Claim>
    {
        new Claim(UserCenterClaimTypes.TokenOrigin, TokenOrigin.系统.ToString()),
        new Claim(UserCenterClaimTypes.UserId, id),
        new Claim(UserCenterClaimTypes.EmpId, id),
        new Claim(UserCenterClaimTypes.SubSystemKey, system)
    };

            // Generate the system token with an expiration time of 60 days (5259600 seconds)
            return tokenGenerator.Generator(systemClaims, "5259600").Token;
        }
    }
}
